import datetime

from fastapi import APIRouter

from apps.allsubdb.baikelib.models import SaveClassModel, SaveArticleModel
from apps.core.global_model import ParseHtmlModel
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, SaveStepMongoModel, SUCCESS, ReturnInfo, DetailModel
from apps.sql_app.mmongodb import Coll

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/step1/save_home")
async def save_home(input: InputInfoModel[ParseHtmlModel]):
    return_info = ReturnInfo()
    task_name = "baikelib"
    step_name = "home"
    html = input.data.html
    id_ = "_".join([task_name, step_name])
    step_info = Coll.get_step_info()
    cjip = await step_info.find_one({"_id": id_})
    if cjip:
        dicts = SaveStepMongoModel[str].parse_obj(cjip).dict()
        dicts["step_info"] = html
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[str].parse_obj(dicts)
        result = await step_info.replace_one({'_id': id_}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[str](_id=id_,
                                       task_name=task_name,
                                       step_name=step_name,
                                       step_info=html,
                                       update_time=str(datetime.datetime.now()),
                                       create_time=str(datetime.datetime.now())
                                       )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step2/save_class_list")
async def save_class_list(input: InputInfoModel[SaveClassModel]):
    """
    保存分类列表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    task_name = "baikelib"
    step_name = "category_page"
    html = input.data.html
    ids = input.data.id
    page = input.data.page
    full_class = input.data.full_class
    id_ = "_".join([task_name, step_name,ids,page])
    step_info = Coll.get_step_info()
    cjip = await step_info.find_one({"_id": id_})
    if cjip:
        dicts = SaveStepMongoModel[SaveClassModel].parse_obj(cjip).dict()
        dicts["step_info"]["html"] = html
        dicts["step_info"]["full_class"] = full_class
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[SaveClassModel].parse_obj(dicts)
        result = await step_info.replace_one({'_id': id_}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[SaveClassModel](
            _id=id_,
            task_name=task_name,
            step_name=step_name,
            step_info=SaveClassModel(
                id=ids,
                html=html,
                page=page,
                full_class=full_class
            ),
            update_time=str(datetime.datetime.now()),
            create_time=str(datetime.datetime.now())
        )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step3/save_article")
async def save_article(input: InputInfoModel[SaveArticleModel]):
    """
    保存文章
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    task_name = "baikelib"
    step_name = "article"
    html = input.data.html
    ids = input.data.id
    name = input.data.name
    parent_code = input.data.parent_code
    parent_class = input.data.parent_class
    full_code = input.data.full_code
    full_class = input.data.full_class
    id_ = "_".join([task_name, step_name,name, ids])
    other = Coll.get_other()
    cjip = await other.find_one({"_id": id_})
    down_date = str(datetime.datetime.now())
    if cjip:
        dicts = DetailModel[SaveArticleModel].parse_obj(cjip).dict()
        dicts["step_info"]["html"] = html
        dicts["step_info"]["name"] = name
        dicts["step_info"]["parent_code"] = parent_code
        dicts["step_info"]["parent_class"] = parent_class
        dicts["step_info"]["full_code"] = full_code
        dicts["step_info"]["full_class"] = full_class
        dicts["update_time"] = str(datetime.datetime.now())
        dicts["down_date"] = down_date
        save = DetailModel[SaveArticleModel].parse_obj(dicts)
        result = await other.replace_one({'_id': id_}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = DetailModel[SaveArticleModel](
            _id=id_,
            task_name=task_name,
            step_name=step_name,
            step_info=SaveArticleModel(
                id=ids,
                name=name,
                parent_code=parent_code,
                parent_class=parent_class,
                full_code=full_code,
                full_class=full_class,
                html=html,
            ),
            downdate=down_date,
            update_time=str(datetime.datetime.now()),
            create_time=str(datetime.datetime.now())
        )
        result = await other.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()
